﻿Imports Microsoft.Practices.EnterpriseLibrary.Logging

Partial Class Admin
    Inherits System.Web.UI.Page
    Const Category As String = "Admin.aspx"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("User") Is Nothing Then
            Session("ErrorMessage") = "Session expired. Please login!"
            Response.Redirect("Error.aspx")
        End If
    End Sub

    Protected Sub SavePassword_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SavePassword.Click
        Const LogTitle As String = "SavePassword"
        Dim LogProperties As Dictionary(Of String, Object)
        Logger.Write(LogTitle & " -> Start.", Category, 10, 0, Diagnostics.TraceEventType.Start, LogTitle)

        If Session("User") Is Nothing Then
            Logger.Write("Session expired.", Category, 10, 0, Diagnostics.TraceEventType.Verbose, LogTitle)
            Session("ErrorMessage") = "Session expired. Please login!"
            Response.Redirect("Error.aspx")
        Else
            Dim Username As String = CStr(Session("User")).ToLower
            LogProperties = General.CreateLogProperties(Username)
            If Password1.Text = Password2.Text Then
                Logger.Write("Entered same password two times correct.", Category, 10, 0, Diagnostics.TraceEventType.Verbose, LogTitle, LogProperties)
                Dim PasswordHash As String = General.EncryptStringToHash(Password1.Text)
                Logger.Write("New Password Hash for user '" & Username & "' : '" & PasswordHash & "'.", Category, 10, 0, Diagnostics.TraceEventType.Verbose, LogTitle, LogProperties)
                Dim Updated As String = XMLFile.SavePassword(Username, PasswordHash)
                If Updated = String.Empty Then
                    Logger.Write("Updated XML-file successfully.", Category, 100, 0, Diagnostics.TraceEventType.Information, LogTitle, LogProperties)
                    Session("GlobalMessage") = "New Password saved sucessfully!"
                    Response.Redirect("Admin.aspx")
                Else
                    Logger.Write("Error updating XML file: '" & Updated & "'.", Category, 200, 0, Diagnostics.TraceEventType.Warning, LogTitle, LogProperties)
                    Session("ErrorMessage") = Updated
                    Response.Redirect("Error.aspx")
                End If
            Else
                Logger.Write("Password not repeated correct!", Category, 100, 0, Diagnostics.TraceEventType.Information, LogTitle, LogProperties)
                Session("ErrorMessage") = "Password was not repeated correct. Please enter two times the same password. " & _
                    "Your password was NOT changed now!"
                Response.Redirect("Error.aspx")
            End If
        End If
    End Sub

  
End Class
